﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace DFGK_WMS.Repository
{
    public class TStationRepository : BaseRepository<StationDto>, ITStationRepository
    {
        public TStationRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }
        /// <summary>
        /// 添加站台信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddStation(StationDto obj)
        {
            try
            {
                TStation sta = new TStation();
                sta = Mapper.Map<TStation>(obj);
                sta.CreateUserId = obj.NowUserId;
                sta.CreateUserName = obj.NowUserRealName;
                sta.CreateDate = DateTime.Now;
                Context.TStations.Add(sta);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除站台信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelStation(int uid)
        {
            try
            {
                var del = Context.TStations.Find(uid);
                if (del != null)
                {
                    Context.TStations.Remove(del);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id获取站台信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TStation GetStationById(int id)
        {
            try
            {
                return Context.TStations.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示站台信息
        /// </summary>
        /// <param name="zcode"></param>
        /// <param name="zname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<StationDto> GetStationList(string cname, int kid, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var alist = Context.TStations.AsQueryable();
                var blist = Context.TWarehouses.AsQueryable();
                if (!string.IsNullOrEmpty(cname))
                {
                    alist = alist.Where(c => c.Name.Contains(cname));
                }
                if (kid > 0)
                {
                    alist = alist.Where(x => x.WhId == kid);
                }
                var query = from A in alist
                            join B in blist
                            on A.WhId equals B.Id
                            select new StationDto
                            {
                                Id = A.Id,
                                Code = A.Code,
                                Name = A.Name,
                                DeviceCode = A.DeviceCode,
                                WhId = A.WhId,
                                WhCode = A.WhCode,
                                WhName = A.WhName,
                                AreaId = A.AreaId,
                                AreaName = A.AreaName,
                                Ledid = A.Ledid,
                                StationSort = A.StationSort,
                                StationType = A.StationType,
                                FunctionType = A.FunctionType,
                                Status = A.Status,
                                IsLock = A.IsLock,
                                AddrFloor = A.AddrFloor,
                                Remark = A.Remark,
                                DisplayOrder = A.DisplayOrder,
                                CreateDate = A.CreateDate,
                                CreateUserId = A.CreateUserId,
                                CreateUserName = A.CreateUserName,
                                ModifyDate = A.ModifyDate,
                                ModifyUserId = A.ModifyUserId,
                                ModifyUserName = A.ModifyUserName,
                                KId=B.Id,
                                KName = B.Name
                            };
                totalCount = query.Count();
                query = query.OrderByDescending(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return query.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取库房(仓库)信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TWarehouse> GetTwarehouseList()
        {
            try
            {
                return Context.TWarehouses.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改站台信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdateStation(StationDto obj)
        {
            try
            {
                TStation sta = new TStation();
                sta = Mapper.Map<TStation>(obj);
                obj.ModifyUserId = obj.NowUserId;
                obj.NowUserRealName = obj.NowUserRealName;
                obj.ModifyDate = DateTime.Now;
                Context.TStations.Update(sta);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据物料名称查询物料信息
        /// </summary>
        /// <param name="wname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TMaterial> GetTmaterialList(string wname, int pageIndex, int pageSize,out int totalCount)
        {
            try
            {
                var list = Context.TMaterials.AsQueryable();
                if (!string.IsNullOrEmpty(wname))
                {
                    list = list.Where(c => c.Name.Contains(wname));
                }
                totalCount = list.Count();
                list = list.OrderBy(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <returns></returns>
        public List<TMaterial> GetTmaterList()
        {
            try
            {
                return Context.TMaterials.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加站点数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddTmater(MaterDto obj)
        {
            try
            {
                TStationDatum sta = new TStationDatum();
                sta = Mapper.Map<TStationDatum>(obj);
                obj.CreateDate = DateTime.Now;
                Context.TStationData.Add(sta);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 货品明细
        /// </summary>
        /// <param name="zcode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TStationDatum> GetMaterList(string zcode, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TStationData.AsQueryable();
                if (!string.IsNullOrEmpty(zcode))
                {
                    list = list.Where(c => c.StationCode.Contains(zcode));
                }
                totalCount = list.Count();
                list = list.OrderBy(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导出货品明细Excel
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<MaterDto> GetExcel()
        {
            try
            {
                var query = Context.TStationData.AsQueryable();
                var table = from s in query
                            select new MaterDto
                            {
                                StationId = s.StationId,
                                StationCode = s.StationCode,
                                Id = s.Id,
                                PackageBarcode = s.PackageBarcode,
                                Barcode = s.Barcode,
                                MaterialId = s.MaterialId,
                                MaterialKeyId = s.MaterialKeyId,
                                BatchNo = s.BatchNo,
                                IsStocktake = s.IsStocktake,
                                CreateDate = s.CreateDate
                            };
                return table.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 带条件查询站台
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<TStation> GetStationList(Expression<Func<TStation,bool>> where)
        {
            return Context.TStations.Where(where).ToList();
        }
    }
}
